<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\helper;

class UrlHelper
{

    /**
     * 添加查询参数。
     *
     * @param unknown $url
     * @param unknown $params
     * @return string
     */
    public static function add_params($url, $params)
    {
        if (empty($url)) {
            $url = request()->url();
        }

        $parts = parse_url($url);

        if (isset($parts['query'])) {

            $query = array();

            foreach (explode('&', $parts['query']) as $item) {
                list($key, $val) = explode('=', $item);
                $query[$key] = $val;
            }

            foreach ($params as $key => $val) {
                $query[$key] = urlencode($val);
            }

            $parts['query'] = http_build_query($query);

        } else {
            $parts['query'] = http_build_query($params);
        }

        $scheme   = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
        $host     = isset($parts['host']) ? $parts['host'] : '';
        $port     = isset($parts['port']) ? ':' . $parts['port'] : '';
        $user     = isset($parts['user']) ? $parts['user'] : '';
        $pass     = isset($parts['pass']) ? ':' . $parts['pass']  : '';
        $pass     = ($user || $pass) ? "$pass@" : '';
        $path     = isset($parts['path']) ? $parts['path'] : '';
        $query    = isset($parts['query']) ? '?' . $parts['query'] : '';
        $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
        return "$scheme$user$pass$host$port$path$query$fragment";
    }

    public static function build_url($url, $args = [], $exclude = [])
    {

    }
}
